package com.redtea.studyspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.redtea.studyspringboot.domain.bo.DeptInfoBo;
import com.redtea.studyspringboot.domain.bo.UserDeptBo;
import com.redtea.studyspringboot.domain.bo.UserInfoBo;
import com.redtea.studyspringboot.domain.vo.UserInfoVo;
import com.redtea.studyspringboot.mapper.DeptMapper;
import com.redtea.studyspringboot.mapper.UserMapper;
import com.redtea.studyspringboot.service.ILoginService;
import lombok.RequiredArgsConstructor;
import org.hibernate.service.spi.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: StudySpringBoot
 * @ClassName LoginService
 * @author: LGP
 * @create: 2024-04-11 18:14
 * @description:
 **/

@RequiredArgsConstructor
@Service
public class LoginServiceImpl extends ServiceImpl<UserMapper, UserInfoBo> implements ILoginService {
    private final UserMapper userMapper;
    private final DeptMapper deptMapper;

    @Override
    public UserInfoVo getUserInfoById(Long id) {
        UserInfoVo userInfoVo = userMapper.queryUserInfoById(id);
        return userInfoVo;
    }

    @Override
    public List<UserInfoVo> getUserInfoList() {
        List<UserInfoVo> userInfoVos = userMapper.queryUserInfoList();
        return userInfoVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addUser(UserDeptBo userDeptBo) {
        return addOneUser(userDeptBo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchAddUser(List<UserDeptBo> userDeptBoList) {

        //这样插入，两表毫无关联
        List<UserInfoBo> userInfoBoList = new ArrayList<>();
        List<DeptInfoBo> deptInfoBoList = new ArrayList<>();
        for (UserDeptBo userDeptBo : userDeptBoList) {
            UserInfoBo userInfoBo = userDeptBo.getUserInfoBo();
            userInfoBoList.add(userInfoBo);

            DeptInfoBo deptInfoBo = userDeptBo.getDeptInfoBo();
            deptInfoBo.setId(userInfoBo.getDeptId());
            deptInfoBoList.add(deptInfoBo);
        }
        boolean saveBatchUser = Db.saveBatch(userInfoBoList);

        for (int i = 0; i < userInfoBoList.size(); i++) {
            UserInfoBo userInfoBo = userInfoBoList.get(i);
            DeptInfoBo deptInfoBo = deptInfoBoList.get(i);
            deptInfoBo.setUserId(userInfoBo.getId());
            System.out.println("uid:" + userInfoBo.getId());
        }
        boolean saveBatchDept = Db.saveBatch(deptInfoBoList);

        for (int i = 0; i < userInfoBoList.size(); i++) {
            UserInfoBo userInfoBo = userInfoBoList.get(i);
            DeptInfoBo deptInfoBo = deptInfoBoList.get(i);
            userInfoBo.setDeptId(deptInfoBo.getId());
            System.out.println("did:" + deptInfoBo.getId());
        }
        boolean updateUserBatch = Db.updateBatchById(userInfoBoList);

        if (!saveBatchUser||!updateUserBatch) {
            throw new ServiceException("用户批量添加失败");
        }
        if (!saveBatchDept) {
            throw new ServiceException("部门批量添加失败");
        }
        return saveBatchUser && saveBatchDept && updateUserBatch;
//        return saveBatchUser;
    }

    /**
     * 更新单个对象，用于编辑场景
     *
     * @param userDeptBo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateUser(UserDeptBo userDeptBo) {
        UserInfoBo userInfoBo = userDeptBo.getUserInfoBo();
        DeptInfoBo deptInfoBo = userDeptBo.getDeptInfoBo();
        //更新人员表
        UpdateWrapper<UserInfoBo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pwd", "12345");
        int update = userMapper.update(userInfoBo, updateWrapper);
        //法二
        boolean supdate = Db.saveOrUpdate(userInfoBo);

        //更新部门表
        int updateDept = deptMapper.update(null, Wrappers.<DeptInfoBo>lambdaUpdate()
                .set(DeptInfoBo::getDeptName, deptInfoBo.getDeptName())
                .eq(DeptInfoBo::getId, userInfoBo.getDeptId())
        );
        //法二
        deptInfoBo.setId(userInfoBo.getDeptId());
        deptInfoBo.setDeptName(deptInfoBo.getDeptName() + "法尔");
        int updateById = deptMapper.updateById(deptInfoBo);

        System.out.println("更新：" + update + " save:" + supdate + " updateDept:" + updateDept + " " + updateById);
        return supdate;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchUpdateUser(List<UserDeptBo> userDeptBoList) {
        //db方式
        ArrayList<UserInfoBo> userInfoBoList = new ArrayList<>();
        ArrayList<DeptInfoBo> deptInfoBoList = new ArrayList<>();
        for (UserDeptBo userDeptBo : userDeptBoList) {
            UserInfoBo userInfoBo = userDeptBo.getUserInfoBo();
            userInfoBoList.add(userInfoBo);
            DeptInfoBo deptInfoBo = userDeptBo.getDeptInfoBo();
            deptInfoBo.setId(userInfoBo.getDeptId());
            deptInfoBoList.add(deptInfoBo);
        }
        boolean userListUpdate = Db.updateBatchById(userInfoBoList);
        boolean deptListUpdate = Db.updateBatchById(deptInfoBoList);
        System.out.println(" 批量更新用户：" + userListUpdate + " 批量更新部门：" + " " + deptListUpdate);
        //xml方式

        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delUser(UserDeptBo userDeptBo) {
        //已经删除的再删就会删除失败
        UserInfoBo userInfoBo = userDeptBo.getUserInfoBo();
        DeptInfoBo deptInfoBo = userDeptBo.getDeptInfoBo();
        deptInfoBo.setId(userInfoBo.getDeptId());
        int delete = userMapper.deleteById(userInfoBo);
//        int delDept = deptMapper.deleteById(deptInfoBo.getId());
        //法二
//        boolean del = Db.removeById(deptInfoBo.getId(), DeptInfoBo.class);
        //法三
        boolean b = Db.removeById(deptInfoBo);
        System.out.println(" 删除用户：" + delete + " 删除部门：" + " " + b);
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDelUser(List<UserDeptBo> userDeptBoList) {
        List<Long> userInfoBoList = new ArrayList<>();
        List<Long> deptInfoBoList = new ArrayList<>();
        for (UserDeptBo userDeptBo : userDeptBoList) {
            UserInfoBo userInfoBo = userDeptBo.getUserInfoBo();
            userInfoBoList.add(userInfoBo.getId());
            DeptInfoBo deptInfoBo = userDeptBo.getDeptInfoBo();
            deptInfoBo.setId(userInfoBo.getDeptId());
            deptInfoBoList.add(userInfoBo.getDeptId());
        }
        int i = userMapper.deleteBatchIds(userInfoBoList);
        int ids = deptMapper.deleteBatchIds(deptInfoBoList);
        boolean result = true;
        if (i != 2) {
            result = false;
            throw new ServiceException("删除用户执行异常");
        } else if (ids != 2) {
            result = false;
            throw new ServiceException("删除部门执行异常");
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean userLogin(String userName, String pwd) {
        return null;
    }

    private Boolean addOneUser(UserDeptBo userDeptBo) {
        UserInfoBo userInfoBo = userDeptBo.getUserInfoBo();
        DeptInfoBo deptInfoBo = userDeptBo.getDeptInfoBo();
/*        UserInfo userInfo = BeanUtil.toBean(userInfoBo, UserInfo.class);
        DeptInfo deptInfo = BeanUtil.toBean(deptInfoBo, DeptInfo.class);*/
        //插入用户
        boolean save = Db.save(userInfoBo);//有id更新，无id插入
//        int userInsert = userMapper.insert(userInfoBo);
        Long userId = userInfoBo.getId();
        //插入部门
        deptInfoBo.setUserId(userId);
        int deptInsert = deptMapper.insert(deptInfoBo);
        Long deptId = deptInfoBo.getId();

        userInfoBo.setDeptId(deptId);
        //更新用户ById
        int updateById = userMapper.updateById(userInfoBo);
        System.out.println("插入结果：" + (save && deptInsert == 1) + " userId:" + userId + " deptId:" + deptId + " updateById:" + updateById);

        if (save && deptInsert == 1) {
            return true;
        } else {
            return false;
        }
    }

}
